const OSS = require("ali-oss");

// class OssServer{
//   client;
//   constructor(){
//     this.
//   }

//   static async bucket(){
//     const list = await this.client.listBuckets()
//     console.log(list)
//   }

//   static async putFile(fileName, fileEntity){
//     const res = await this.client.put(fileName, fileEntity);
//     return {
//       url: res.url,
//       size: res.res.size,
//     };
//   }
// }

const client = new OSS({
  region: "oss-cn-hangzhou",
  accessKeyId: "LTAI5tNJ9y81X5jwwVWBMbAP",
  accessKeySecret: "KxHW77DvDb6aW6bke6iDnKCjIOaabE",
  bucket: "yijian-alpha-oss",
  timeout: '1200000ms'
});

const fileOperation = {
  putFile: async (fileName, fileEntity) => {
    try {
      // console.log({
      //   fileName,
      //   fileEntity,
      // });
      const res = await client.put(fileName, fileEntity);
      const setPublic = await  client.putACL(fileName, 'public-read');
      // const url = client.signatureUrl(fileName,{ expires: Date.now() + 24 * 3600 * 1000 * 30 * 12})
      console.log("OSS UPDATE PUBLIC ------->",setPublic);
      // console.log("@res------->",res.url);
      // console.log("@res2------->",res2);
      const url = res.url.replace('http://','https://')
      return url
    } catch (err) {
      console.error("@", err);
      throw Error("shibai !!!!",err)
    }
  },

};

module.exports = fileOperation;
